package com.mdc.mansion.web.modules.dishes.web;

import com.mdc.mansion.core.modules.restaurant.RestaurantService;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.mdc.mansion.web.modules.system.utils.UserUtils;
import com.mdc.mansion.dataaccess.modules.system.entity.User;

import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishes;
import com.mdc.mansion.dataaccess.modules.dishes.vo.RestaurantDishesVo;
import com.esotericsoftware.minlog.Log;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesService;
import com.mdc.mansion.web.common.web.BaseController;
/**
 * 餐厅菜品信息Controller
 * @author mingdongc
 * @version 2018-01-01
 */
@Controller
@RequestMapping(value = "${adminPath}/dishes/restaurantDishes")
public class RestaurantDishesController extends BaseController {

	@Autowired
	private RestaurantDishesService restaurantDishesService;

	@Autowired
	private RestaurantService restaurantService;
	
	@ModelAttribute
	public RestaurantDishes get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return restaurantDishesService.get(id);
		}else{
			return new RestaurantDishes();
		}
	}
	
	@RequiresPermissions("dishes:restaurantDishes:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(RestaurantDishes restaurantDishes) {
		Map<String, Object> model=new HashMap<String, Object>();
		Page<RestaurantDishes> page = restaurantDishesService.findPage(restaurantDishes);
        model.put("page", page);
		model.put("restaurants",restaurantService.findList(new Restaurant()));
		ModelAndView modelAndView=new ModelAndView("modules/dishes/restaurantDishesList", model);
		return modelAndView;
	}
	
	/**
	 * 根据前台页面提供参数: 菜单名称，是否套餐，等信息进行菜单信息查询并可以修改套餐翻译等
	 * @param restaurantDishesVo
	 * @return
	 */
	@RequiresPermissions("dishes:restaurantDishes:view")
	@RequestMapping(value = {"findDishesInfoPage"})
	public ModelAndView findDishesInfo(RestaurantDishesVo restaurantDishesVo) {
		Map<String, Object> model = null;
		Page<RestaurantDishes> page = null;
		
		try{
			model=new HashMap<String, Object>();
			page = restaurantDishesService.findDishesInfoPage(restaurantDishesVo);
	        model.put("page", page);
		}catch(Exception e)
		{
			Log.error(e.getMessage());
		}
        ModelAndView modelAndView=new ModelAndView("modules/dishes/restaurantDishesList", model);
		return modelAndView;
	}
	
	@RequiresPermissions("dishes:restaurantDishes:view")
	@RequestMapping(value = "form")
	public ModelAndView form(RestaurantDishes restaurantDishes) {
		Map<String, Object> model=new HashMap<String, Object>();
		model.put("restaurantDishes", restaurantDishes);
		return new ModelAndView("modules/dishes/restaurantDishesForm",model);
	}
	

	@RequiresPermissions("dishes:restaurantDishes:edit")
	@RequestMapping(value = "save")
	public ModelAndView save(RestaurantDishes restaurantDishes,RedirectAttributes redirectAttributes) {
		User u=UserUtils.getUser();
		restaurantDishes.setUpdateBy(u);
		if (StringUtils.isNotBlank(restaurantDishes.getId())){
			restaurantDishesService.updateSelective(restaurantDishes);
		}else{
			restaurantDishes.setCreateBy(u);
			restaurantDishesService.save(restaurantDishes);
		}
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	@RequiresPermissions("dishes:restaurantDishes:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(RestaurantDishes restaurantDishes,RedirectAttributes redirectAttributes) {
		restaurantDishesService.delete(restaurantDishes);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
}
